#include "debug.h"
#include "../DDIC/AllDDIC.h"
#include "usbd_cdc.h"

u8 RM692D9_Gamma_R[62]=
{
		0x14,0x15,0x1A,0x1B,0x20,0x21,0x26,0x27,0x2D,0x2F,0x34,0x35,0x3A,0x3B,0x42,0x43,0x48,0x49,0x4E,0x4F,0x54,0x55,
	  0x5B,0x5C,0x61,0x62,0x67,0x68,0x6D,0x6E,0x73,0x74,0x79,0x7A,0x7F,0x80,0x85,0x86,0x8B,0x8C,0x91,0x92,0x97,0x98,
	  0x9D,0x9E,0xA4,0xA5,0xAB,0xAC,0xb1,0xb2,0xb7,0xb8,0xbd,0xbe,0xc3,0xc4,0xc9,0xca,0xcf,0xd0
};
u8 RM692D9_Gamma_G[62]=
{
		0x16,0x17,0x1C,0x1D,0x22,0x23,0x28,0x29,0x30,0x31,0x36,0x37,0x3D,0x3F,0x44,0x45,0x4A,0x4B,0x50,0x51,0x56,0x58,
	  0x5D,0x5E,0x63,0x64,0x69,0x6A,0x6F,0x70,0x75,0x76,0x7B,0x7C,0x81,0x82,0x87,0x88,0x8D,0x8E,0x93,0x94,0x99,0x9A,
	  0x9F,0xA0,0xA6,0xA7,0xAD,0xAE,0xb3,0xb4,0xb9,0xba,0xbf,0xc0,0xc5,0xc6,0xcb,0xcc,0xd1,0xd2
};
u8 RM692D9_Gamma_B[62]=
{
	 0x18,0x19,0x1E,0x1F,0x24,0x25,0x2A,0x2B,0x32,0x33,0x38,0x39,0x40,0x41,0x46,0x47,0x4C,0x4D,0x52,0x53,0x59,0x5A,
	 0x5F,0x60,0x65,0x66,0x6B,0x6C,0x71,0x72,0x77,0x78,0x7D,0x7E,0x83,0x84,0x89,0x8A,0x8F,0x90,0x95,0x96,0x9B,0x9C,
	 0xA2,0xA3,0xA9,0xAA,0xAF,0xB0,0xb5,0xb6,0xbb,0xbc,0xc1,0xc2,0xc7,0xc8,0xcd,0xce,0xd3,0xd4
};

u8 RM692D9_Gamma_R15[30]=
{
		0x14,0x15,0x1A,0x1B,0x20,0x21,0x26,0x27,0x2D,0x2F,0x34,0x35,
	  0x3A,0x3B,0x42,0x43,0x48,0x49,0x4E,0x4F,0x54,0x55,0x5B,0x5C,0x61,0x62,0x67,0x68,0x6d,0x6e
};
u8 RM692D9_Gamma_G15[30]=
{
		0x16,0x17,0x1C,0x1D,0x22,0x23,0x28,0x29,0x30,0x31,0x36,0x37,
	  0x3D,0x3F,0x44,0x45,0x4A,0x4B,0x50,0x51,0x56,0x58,0x5D,0x5E,0x63,0x64,0x69,0x6A,0x6F,0x70
};
u8 RM692D9_Gamma_B15[30]=
{
	  0x18,0x19,0x1E,0x1F,0x24,0x25,0x2A,0x2B,0x32,0x33,0x38,0x39,
	  0x40,0x41,0x46,0x47,0x4C,0x4D,0x52,0x53,0x59,0x5A,0x5F,0x60,0x65,0x66,0x6b,0x6c,0x71,0x72
};



u8 RM692D9_Gamma_Points[33] =
{
	 0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,0x50,
	 0x51,0x52,0x53,0x54,0x55,0x56,0x58,0x59,
	 0x5a,0x5b,0x5c,0x5d,0x5e,0x5f,0x60,0x61,
	 0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69
};

void RM692D9_Write_Register( USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{
    //SSD2828_W_Reg(SigMode,channel,0xB7,(LP_B7_Data&0xFBFF)|0x0400); 
		SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0410)&0XFFFD);		
    delay_ms(5);	
		SSD2828_W_Array(SigMode,channel,buffer,2);
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    delay_ms(5);
    buffer[4] = Uart_Error_None;
    STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 写寄存器状态  ：ok
}

void RM692D9_Read_Register( USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{
//    u16 tmp;
//	u8 buffer1[3];
    SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);                               
    
	delay_ms(5);
	Mipi_Read(pdev,SigMode,channel,buffer[3],buffer[2]-1,Buffer_ReadCode);
	memcpy(buffer+4,Buffer_ReadCode,buffer[2]-1);
    
	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
	buffer[1]=2;  //0x02 Read标志位，串口返回数据处理部分
    STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 读取的 寄存器数据  ：ok 
}


void RM692D9_Write_Gamma( USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 buffer1[5];
    SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFfd);
		delay_ms(2);
			
//----------------------------------------------------------	
		buffer1[0] = 0x02;
    if((buffer[3]&0x0f) == 0x00)
    {
        buffer1[1] = 0xFE;
        buffer1[2] = 0x50;                // GAMMA1  page1
        SSD2828_W_Array(SigMode,channel,buffer1,0);
    }
		else if((buffer[3]&0x0f) == 0x04)
    {
        buffer1[1] = 0xFE;
        buffer1[2] = 0x56;                // GAMMAX UDC
        SSD2828_W_Array(SigMode,channel,buffer1,0);
    }		
		else if((buffer[3]&0x0f) == 0x03)
    {
        buffer1[1] = 0xFE;
        buffer1[2] = 0x52;                // 动态 ELVDD   52page
        SSD2828_W_Array(SigMode,channel,buffer1,0);
    }	
    for( i = 0;i<buffer[2]-3;i++) 
    {	 
        if((buffer[3]&0xf0)==0x00)	
            SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[i+6]<<8)|RM692D9_Gamma_R15[i]);  
        else if((buffer[3]&0xf0)==0x40)
            SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[i+6]<<8)|RM692D9_Gamma_G15[i]); 
        else if((buffer[3]&0xf0)==0x80)
            SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[i+6]<<8)|RM692D9_Gamma_B15[i]); 																		
        delay_ms(2);
    }
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    buffer[4] = Uart_Error_None;
    STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 写Gamma状态  ：ok  
}

void RM692D9_Write_Gamma31( USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 buffer1[5];
    SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFfd);
		delay_ms(2);
			
//----------------------------------------------------------	
		buffer1[0] = 0x02;
    if((buffer[3]&0x0f) == 0x00)
    {
        buffer1[1] = 0xFE;
        buffer1[2] = 0x50;                // GAMMA1  page1
        SSD2828_W_Array(SigMode,channel,buffer1,0);
    }
		else if((buffer[3]&0x0f) == 0x04)
    {
        buffer1[1] = 0xFE;
        buffer1[2] = 0x56;                // GAMMAX UDC
        SSD2828_W_Array(SigMode,channel,buffer1,0);
    }		
		else if((buffer[3]&0x0f) == 0x03)
    {
        buffer1[1] = 0xFE;
        buffer1[2] = 0x52;                // 
        SSD2828_W_Array(SigMode,channel,buffer1,0);
    }		
    for( i = 0;i<buffer[2]-3;i++) 
    {	 
        if((buffer[3]&0xf0)==0x00)	
            SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[i+6]<<8)|RM692D9_Gamma_R[i]);  
        else if((buffer[3]&0xf0)==0x40)
            SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[i+6]<<8)|RM692D9_Gamma_G[i]); 
        else if((buffer[3]&0xf0)==0x80)
            SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[i+6]<<8)|RM692D9_Gamma_B[i]); 																		
        delay_ms(2);
    }
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    buffer[4] = Uart_Error_None;
    STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 写Gamma状态  ：ok  
}


void RM692D9_Read_Gamma( USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 buffer1[3];
		SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
		delay_ms(10);
	
	 buffer1[0] = 0x02;	
	 buffer1[1] = 0xFE;
	 buffer1[2] = 0x00;      
    if(buffer[4] == 0x01)      //Gamma1
    {			
        buffer1[1] = 0xFE;
			  buffer1[2] = 0x50;      // GAMMA1  page1
        SSD2828_W_Array(SigMode,channel,buffer1,0); 
    }
		else if (buffer[4] ==0x05)
		{
        buffer1[1] = 0xFE;        
        buffer1[2] = 0x56;      // GAMMAX UDC
        SSD2828_W_Array(SigMode,channel,buffer1,0); 
		}			
		else if (buffer[4] ==0x03)
		{
        buffer1[1] = 0xFE;        
        buffer1[2] = 0x52;      // 52 page  
        SSD2828_W_Array(SigMode,channel,buffer1,0); 
		}	
		delay_ms(5);		
    SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);
		delay_ms(5);				
		SSD2828_W_Reg(SigMode,channel,0xC1,0x0002); 
    SSD2828_W_Reg(SigMode,channel,0xBC,0x0001);			
    for( i = 0;i<buffer[2]-3;i++)			 	
    {  	                              
        buffer1[0] = 0x01;
        buffer1[1] = (buffer[3]==0x00) ? RM692D9_Gamma_R15[i] : ((buffer[3]==0x42) ? RM692D9_Gamma_G15[i] : RM692D9_Gamma_B15[i]);
        SSD2828_W_Array(SigMode,channel,buffer1,0); 
				delay_ms(6);
        buffer[6+i]=SSD2828_R_Reg(SigMode,channel,0xFF);        
    }
    delay_ms(5);
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 读取的 Gamma数据  ：ok  
}

void RM692D9_Read_Gamma31( USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 buffer1[3];
		SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
		delay_ms(10);
	
	 buffer1[0] = 0x02;	
	 buffer1[1] = 0xFE;
	 buffer1[2] = 0x00;      
    if(buffer[4] == 0x01)      //Gamma1
    {			
        buffer1[1] = 0xFE;
			  buffer1[2] = 0x50;      // GAMMA1  page1
        SSD2828_W_Array(SigMode,channel,buffer1,0); 
    }
		else if (buffer[4] ==0x05)
		{
        buffer1[1] = 0xFE;        
        buffer1[2] = 0x56;      // GAMMAX UDC
        SSD2828_W_Array(SigMode,channel,buffer1,0); 
		}			
		else if (buffer[4] ==0x03)
		{
        buffer1[1] = 0xFE;        
        buffer1[2] = 0x52;      // 52 page  
        SSD2828_W_Array(SigMode,channel,buffer1,0); 
		}	
		delay_ms(5);		
    SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);
		delay_ms(5);				
		SSD2828_W_Reg(SigMode,channel,0xC1,0x0002); 
    SSD2828_W_Reg(SigMode,channel,0xBC,0x0001);			
    for( i = 0;i<buffer[2]-3;i++)			 	
    {  	                              
        buffer1[0] = 0x01;
        buffer1[1] = (buffer[3]==0x00) ? RM692D9_Gamma_R[i] : ((buffer[3]==0x42) ? RM692D9_Gamma_G[i] : RM692D9_Gamma_B[i]);
        SSD2828_W_Array(SigMode,channel,buffer1,0); 
				delay_ms(6);
        buffer[6+i]=SSD2828_R_Reg(SigMode,channel,0xFF);        
    }
    delay_ms(5);
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 读取的 Gamma数据  ：ok  
}

void RM692D9_AutoGamma_UDC( USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{
	  u8 buffer1[3];
    if(SigMode == Video_Mode)
    {
        SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);
    }
    else if(SigMode == CMD_Mode)	
		{
		    SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
		}
		delay_ms(5);
		


		buffer1[0] = 0x02;	
		buffer1[1] = 0xFE;
		buffer1[2] = 0x56;                
		SSD2828_W_Array(SigMode,channel,buffer1,0);		
		
		SSD2828_W_Reg(SigMode,channel,0xBC,0x02);
		delay_ms(2);
		SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[5]<<8)|RM692D9_Gamma_R15[30-2-2*buffer[4]]);     //R
		delay_ms(2);
		SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[6]<<8)|RM692D9_Gamma_R15[30-1-2*buffer[4]]);     //R
		delay_ms(2);
	  SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[7]<<8)|RM692D9_Gamma_G15[30-2-2*buffer[4]]);     //G
		delay_ms(2);
		SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[8]<<8)|RM692D9_Gamma_G15[30-1-2*buffer[4]]);     //G
		delay_ms(2);
		SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[9]<<8)|RM692D9_Gamma_B15[30-2-2*buffer[4]]);     //B
		delay_ms(2);
		SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[10]<<8)|RM692D9_Gamma_B15[30-1-2*buffer[4]]);     //B
		delay_ms(2);
		
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
}

void RM692D9_AutoGamma_UDC_31( USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{
	  u8 buffer1[3];
    if(SigMode == Video_Mode)
    {
        SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);
    }
    else if(SigMode == CMD_Mode)	
		{
		    SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
		}
		delay_ms(5);
		


		buffer1[0] = 0x02;	
		buffer1[1] = 0xFE;
		buffer1[2] = 0x56;                
		SSD2828_W_Array(SigMode,channel,buffer1,0);		
		
		SSD2828_W_Reg(SigMode,channel,0xBC,0x02);
		delay_ms(2);
		SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[5]<<8)|RM692D9_Gamma_R[62-2-2*buffer[4]]);     //R
		delay_ms(2);
		SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[6]<<8)|RM692D9_Gamma_R[62-1-2*buffer[4]]);     //R
		delay_ms(2);
	  SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[7]<<8)|RM692D9_Gamma_G[62-2-2*buffer[4]]);     //G
		delay_ms(2);
		SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[8]<<8)|RM692D9_Gamma_G[62-1-2*buffer[4]]);     //G
		delay_ms(2);
		SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[9]<<8)|RM692D9_Gamma_B[62-2-2*buffer[4]]);     //B
		delay_ms(2);
		SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[10]<<8)|RM692D9_Gamma_B[62-1-2*buffer[4]]);     //B
		delay_ms(2);
		
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
}

void RM692D9_AutoGamma( USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{
	  u8 buffer1[3];
    if(SigMode == Video_Mode)
    {
        SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);
    }
    else if(SigMode == CMD_Mode)	
		{
		    SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
		}
		delay_ms(5);
		if((buffer[3]&0x03) == 0x01 )
		{
		    buffer1[0] = 0x02;	
				buffer1[1] = 0xFE;
				buffer1[2] = 0x50;   
		}
		else if((buffer[3]&0x03) == 0x02)
		{
				buffer1[0] = 0x02;	
				buffer1[1] = 0xFE;
				buffer1[2] = 0x52;   
			
		}
    delay_ms(3); 
		SSD2828_W_Array(SigMode,channel,buffer1,0);		
		delay_ms(5);
		
		SSD2828_W_Reg(SigMode,channel,0xBC,0x02);
		delay_ms(5);
		SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[5]<<8)|RM692D9_Gamma_R15[30-2-2*buffer[4]]);     //R
		delay_ms(3);
		SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[6]<<8)|RM692D9_Gamma_R15[30-1-2*buffer[4]]);     //R
		delay_ms(3);
	  SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[7]<<8)|RM692D9_Gamma_G15[30-2-2*buffer[4]]);     //G
		delay_ms(3);
		SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[8]<<8)|RM692D9_Gamma_G15[30-1-2*buffer[4]]);     //G
		delay_ms(3);
		SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[9]<<8)|RM692D9_Gamma_B15[30-2-2*buffer[4]]);     //B
		delay_ms(3);
		SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[10]<<8)|RM692D9_Gamma_B15[30-1-2*buffer[4]]);     //B
		delay_ms(3);
				
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
		delay_ms(5);
}


void RM692D9_AutoGamma_31( USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{
	  u8 buffer1[3];
    if(SigMode == Video_Mode)
    {
        SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);
    }
    else if(SigMode == CMD_Mode)	
		{
		    SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
		}
		delay_ms(5);
		if((buffer[3]&0x03) == 0x01 )
		{
		    buffer1[0] = 0x02;	
				buffer1[1] = 0xFE;
				buffer1[2] = 0x50;   
		}
		else if((buffer[3]&0x03) == 0x02)
		{
				buffer1[0] = 0x02;	
				buffer1[1] = 0xFE;
				buffer1[2] = 0x52;   			
		}
		SSD2828_W_Array(SigMode,channel,buffer1,0);		
		
		SSD2828_W_Reg(SigMode,channel,0xBC,0x02);
		delay_ms(2);
		SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[5]<<8)|RM692D9_Gamma_R[62-2-2*buffer[4]]);     //R
		delay_ms(2);
		SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[6]<<8)|RM692D9_Gamma_R[62-1-2*buffer[4]]);     //R
		delay_ms(2);
	  SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[7]<<8)|RM692D9_Gamma_G[62-2-2*buffer[4]]);     //G
		delay_ms(2);
		SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[8]<<8)|RM692D9_Gamma_G[62-1-2*buffer[4]]);     //G
		delay_ms(2);
		SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[9]<<8)|RM692D9_Gamma_B[62-2-2*buffer[4]]);     //B
		delay_ms(2);
		SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[10]<<8)|RM692D9_Gamma_B[62-1-2*buffer[4]]);     //B
		delay_ms(2);
				
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
}


void RM692D9_Gamma_Mapping( USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{
		u8 buffer1[5];
		SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFfd);
		delay_ms(5);
		buffer1[0] = 0x02;
		buffer1[1] = 0xfe;
		buffer1[2] = 0x70;
		SSD2828_W_Array(SigMode,channel,buffer1,0);
	
		buffer1[1] = 0x48;
		buffer1[2] = buffer[3];
		SSD2828_W_Array(SigMode,channel,buffer1,0);		
	
		for( i = 1;i<buffer[2];i++) 
		{	 
			SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[i+3]<<8)|RM692D9_Gamma_Points[i]);   																	
			delay_ms(1);
		}
		SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
		buffer[4] = Uart_Error_None;
		STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 写Gamma状态  ：ok  		

}



void RM692D9_Gamma_OTP_Start( USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{
		u8 buffer1[5];
		SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
		delay_ms(5);


    buffer1[0] = 0x02;
    buffer1[1] = 0xFE;
    buffer1[2] = 0x00;
    SSD2828_W_Array(SigMode,channel,buffer1,0);  		

		buffer1[0] = 0x01;
		buffer1[1] = 0x28;
		SSD2828_W_Array(SigMode,channel,buffer1,0); 	
		delay_ms(50);
		buffer1[0] = 0x01;
		buffer1[1] = 0x10;
		SSD2828_W_Array(SigMode,channel,buffer1,0); 	
		delay_ms(50);	
//----------------------------		

 
//------page Enable-------------------
buffer1[0] = 0x02;
buffer1[1] = 0xFE;
buffer1[2] = 0x40;
SSD2828_W_Array(SigMode,channel,buffer1,0);  
delay_ms(3); 
//gam1 gam5
if((buffer[3]&0x03) == 0x03)    //11111      gam21,gam20,gam9,gam5,gam1
{
	 buffer1[1] = 0xF0;
   buffer1[2] = 0x11;
   SSD2828_W_Array(SigMode,channel,buffer1,0);     
}
else if((buffer[3]&0x03) == 0x02)
{
	 buffer1[1] = 0xF0;
   buffer1[2] = 0x10;
   SSD2828_W_Array(SigMode,channel,buffer1,0);   

}
else if((buffer[3]&0x03) == 0x01)
{
	 buffer1[1] = 0xF0;
   buffer1[2] = 0x01;
   SSD2828_W_Array(SigMode,channel,buffer1,0);   

}
delay_ms(3); 
//gam9
if((buffer[3]&0x04) == 0x04)    //
{
	 buffer1[1] = 0xEF;
   buffer1[2] = 0x01;
   SSD2828_W_Array(SigMode,channel,buffer1,0);     
}
delay_ms(3); 
//gam20 gam21
if((buffer[3]&0x18) == 0x18)    //
{
	 buffer1[1] = 0xEE;
   buffer1[2] = 0x18;
   SSD2828_W_Array(SigMode,channel,buffer1,0);     
}
else if((buffer[3]&0x18) == 0x10)    //gam 21
{
	 buffer1[1] = 0xEE;
   buffer1[2] = 0x10;
   SSD2828_W_Array(SigMode,channel,buffer1,0);     
}
else if((buffer[3]&0x18) == 0x08)    //gam 20
{
	 buffer1[1] = 0xEE;
   buffer1[2] = 0x08;
   SSD2828_W_Array(SigMode,channel,buffer1,0);     
}
delay_ms(3); 
//--------------------	

		buffer1[0] = 0x02;
    buffer1[1] = 0xFE;
    buffer1[2] = 0x42;
    SSD2828_W_Array(SigMode,channel,buffer1,0);   
delay_ms(3); 
		buffer1[1] = 0x1E;
    buffer1[2] = 0x01;
    SSD2828_W_Array(SigMode,channel,buffer1,0);   
delay_ms(3); 
//--------------------
		buffer1[0] = 0x02;
    buffer1[1] = 0xFE;
    buffer1[2] = 0x40;
    SSD2828_W_Array(SigMode,channel,buffer1,0);     //Page0
    delay_ms(50);
    buffer1[0] = 0x02;//MTPCTR
    buffer1[1] = 0xF2;
    buffer1[2] = 0x01;
    SSD2828_W_Array(SigMode,channel,buffer1,0);   
		delay_ms(100); 
    buffer1[0] = 0x02;
    buffer1[1] = 0xF3;
    buffer1[2] = 0xA5;
    SSD2828_W_Array(SigMode,channel,buffer1,0);  
    buffer1[0] = 0x02;
    buffer1[1] = 0xF4;
    buffer1[2] = 0x5A;
    SSD2828_W_Array(SigMode,channel,buffer1,0);  
    buffer1[0] = 0x02;
    buffer1[1] = 0xF5;
    buffer1[2] = 0x3C;
    SSD2828_W_Array(SigMode,channel,buffer1,0);  		
    delay_ms(1500);         // wait for >400 ms   
    buffer[4] = Uart_Error_None;
    buffer[1] = 0x07;
    STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 写OTP状态  ：ok		
		
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);		
}

void RM692D9_WriteToRAM( USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{
		u8 buffer1[5];
		SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
		delay_ms(15);
		buffer1[0] = 0x02;
    buffer1[1] = 0xFe;
    buffer1[2] = 0x40;
    SSD2828_W_Array(SigMode,channel,buffer1,0);  
		delay_ms(3);
		if(buffer[3] == 0x01)
		{
			buffer1[1] = 0xE3;
			buffer1[2] = 0x41;
		}
		else if(buffer[3] == 0x02)
		{
			buffer1[1] = 0xE3;
			buffer1[2] = 0x45;
		}
		else if(buffer[3] == 0x03)
		{
			buffer1[1] = 0xE3;
			buffer1[2] = 0x49;
		}
		else if(buffer[3] == 0x04)
		{
			buffer1[1] = 0xE6;
			buffer1[2] = 0x14;
			SSD2828_W_Array(SigMode,channel,buffer1,0);  
		  delay_ms(3);	
			buffer1[1] = 0xE3;
			buffer1[2] = 0x40;
		}
		else if(buffer[3] == 0x05)
		{
			buffer1[1] = 0xE6;
			buffer1[2] = 0x15;
			SSD2828_W_Array(SigMode,channel,buffer1,0);  
		  delay_ms(3);	
			buffer1[1] = 0xE3;
			buffer1[2] = 0x40;
		}
		
	  
	  SSD2828_W_Array(SigMode,channel,buffer1,0);  
		delay_ms(3);	
	

		SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
		buffer[4] = Uart_Error_None;
		STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 写Gamma状态  ：ok  		

}

//强切
void RM692D9_qiangqie( USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{
		u8 buffer1[5];
		SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
		delay_ms(15);
	  if((buffer[3]&0x03) == 0x01)    //主屏
		{
		   	buffer1[0] = 0x02;
				buffer1[1] = 0xFe;
				buffer1[2] = 0x70;
				SSD2828_W_Array(SigMode,channel,buffer1,0);  
				delay_ms(3);
		    buffer1[1] = 0x71;
				buffer1[2] = 0x00;
				SSD2828_W_Array(SigMode,channel,buffer1,0);  
				delay_ms(3);
				buffer1[1] = 0x72;
				buffer1[2] = 0x31;
				SSD2828_W_Array(SigMode,channel,buffer1,0);  
				delay_ms(3);		
		}
		else if((buffer[3]&0x03) == 0x02)    //副屏
		{
		    buffer1[0] = 0x02;	
				buffer1[1] = 0xFE;
				buffer1[2] = 0x40;               
				SSD2828_W_Array(SigMode,channel,buffer1,0);	
			  delay_ms(3);		
				buffer1[1] = 0xe6;  
				buffer1[2] = 0x14;                // 强切gamma56
				SSD2828_W_Array(SigMode,channel,buffer1,0);			
        delay_ms(3);			
				buffer1[1] = 0xe3;  
				buffer1[2] = 0x40;                // 强切gamma56
				SSD2828_W_Array(SigMode,channel,buffer1,0);	
			  delay_ms(3);
		}
		else if((buffer[3]&0x03) == 0x03)    //副屏
		{
		    buffer1[0] = 0x02;	
				buffer1[1] = 0xFE;
				buffer1[2] = 0x52;               
				SSD2828_W_Array(SigMode,channel,buffer1,0);	
			  delay_ms(3);		
				buffer1[1] = 0x00;
				buffer1[2] = 0x99;               
				SSD2828_W_Array(SigMode,channel,buffer1,0);	
			  delay_ms(3);	
				buffer1[1] = 0x01;
				buffer1[2] = 0xC0;               
				SSD2828_W_Array(SigMode,channel,buffer1,0);	
			  delay_ms(3);	
				buffer1[1] = 0x02;
				buffer1[2] = 0x00;               
				SSD2828_W_Array(SigMode,channel,buffer1,0);	
			  delay_ms(3);	
				buffer1[1] = 0x03;
				buffer1[2] = 0x78;               
				SSD2828_W_Array(SigMode,channel,buffer1,0);	
			  delay_ms(3);	
				buffer1[1] = 0x04;
				buffer1[2] = 0x48;               
				SSD2828_W_Array(SigMode,channel,buffer1,0);	
			  delay_ms(3);	
		}
	

		SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
		buffer[4] = Uart_Error_None;
		STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 写Gamma状态  ：ok  		

}


//----20190713----
//---------------------------------------------------------------------------------------------------------------
void RM692D9_Read_MTP_Time( USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 buffer1[3];	

	u8 Addr[]={0xB4,0xB3,0xB2,0xB7,0xB6,0xB5};
	
	SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0410)&0XFFFD);		
		delay_ms(5);
		SSD2828_W_Reg(SigMode,channel,0xC1,0x0002);    
			buffer1[0] = 0x02;
			buffer1[1] = 0xfe;
			buffer1[2] = 0x44;
			SSD2828_W_Array(SigMode,channel,buffer1,0); 
			delay_ms(20);		
	  for(i=0;i<6;i++)
		{
			SSD2828_W_Reg(SigMode,channel,0xB7,((HS_B7_Data|0x0010)&0XFFFD)|0x0080);  //bit7 1:??? bit4 1:HS CLOCK Disable
			delay_ms(5);					
			SSD2828_W_Reg(SigMode,channel,0xC1,0x0002);                                
			buffer1[0] = 0x01;
			buffer1[1] = Addr[i];
			SSD2828_W_Array(SigMode,channel,buffer1,0); 
			delay_ms(20);
			buffer[4+i]=SSD2828_R_Reg(SigMode,channel,0xFF);
		}
		SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    delay_ms(5);
		
    STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);
}


//---20190713-----RM692D9_Read_All_MTP_Reg
void RM692D9_Read_All_MTP_Reg( USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 SendBuff[3];	
	u8 Addr[21]={0xe7,0xe6,0xAE ,0xAF ,0xB0 ,0xB1 ,0xB4 ,0xB3 ,0xB2 ,0xA9 ,0xAA,0xAB,0xBA,0xBB,0xAD ,0xAC ,0xB7 ,0xB6,0xB5,0xB9 ,0xB8};
	//---READ 0xD0	
	SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0410)&0XFFFD);		
	delay_ms(5);
	
	SSD2828_W_Reg(SigMode,channel,0xC1,0x0002);    
	SendBuff[0] = 0x02;
	SendBuff[1] = 0xfe;
	SendBuff[2] = 0xD0;
	SSD2828_W_Array(SigMode,channel,SendBuff,0); 
	delay_ms(20);		
	for(i=0;i<2;i++)
	{
		SSD2828_W_Reg(SigMode,channel,0xB7,((HS_B7_Data|0x0010)&0XFFFD)|0x0080);  //bit7 1:??? bit4 1:HS CLOCK Disable
		delay_ms(5);					
		SSD2828_W_Reg(SigMode,channel,0xC1,0x0002);                                
		SendBuff[0] = 0x01;
		SendBuff[1] = Addr[i];
		SSD2828_W_Array(SigMode,channel,SendBuff,0); 
		delay_ms(20);
		buffer[4+i]=SSD2828_R_Reg(SigMode,channel,0xFF);
	}
	//---READ 0x44
	SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0410)&0XFFFD);		
	delay_ms(5);
	
	SSD2828_W_Reg(SigMode,channel,0xC1,0x0002);    
	SendBuff[0] = 0x02;
	SendBuff[1] = 0xfe;
	SendBuff[2] = 0x44;
	SSD2828_W_Array(SigMode,channel,SendBuff,0); 
	delay_ms(20);		
	for(i=2;i<21;i++)
	{
		SSD2828_W_Reg(SigMode,channel,0xB7,((HS_B7_Data|0x0010)&0XFFFD)|0x0080);  //bit7 1:??? bit4 1:HS CLOCK Disable
		delay_ms(5);					
		SSD2828_W_Reg(SigMode,channel,0xC1,0x0002);                                
		SendBuff[0] = 0x01;
		SendBuff[1] = Addr[i];
		SSD2828_W_Array(SigMode,channel,SendBuff,0); 
		delay_ms(20);
		buffer[4+i]=SSD2828_R_Reg(SigMode,channel,0xFF);
	}
	
	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    delay_ms(5);
		
    STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);
}
//-----------------------------------------------------------------------------

void ProcessForIc68( USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{	
	//void *pdev;
	switch(buffer[1])
	{
		case 0x01:                                      //写寄存器
				RM692D9_Write_Register(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);
				break;
		case 0x02:                                      //读寄存器
				RM692D9_Read_Register(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);
				break;
		case 0x04:                                      //写Gamma
				RM692D9_Write_Gamma(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);
				break;
		case 0xF4:                                      //写Gamma
				RM692D9_Write_Gamma31(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);
				break;
		case 0x05:                                      //写Gamma
				RM692D9_Read_Gamma(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);
				break;	
		case 0xF5:                                      //写Gamma
				RM692D9_Read_Gamma31(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);
				break;			
		case 0x07:                                      //读Gamma
				RM692D9_Gamma_OTP_Start(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data); 
				break;
		case 0x06: 
				RM692D9_Gamma_Mapping(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);	
				break;
		case 0x0B:
				RM692D9_Read_MTP_Time(pdev,ep_addr,SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data);
				break;
    case 0x1B:
				RM692D9_Read_All_MTP_Reg(pdev,ep_addr,SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data);
				break;
		case 0xDD:   									// auto Gamma数据
			  RM692D9_AutoGamma(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data); 
		break;		
		case 0xfD:   									// auto Gamma数据
			  RM692D9_AutoGamma_31(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data); 
		break;	
		case 0xDC: 
				RM692D9_AutoGamma_UDC(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);
		break;
		case 0xfC: 
				RM692D9_AutoGamma_UDC_31(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);
		break;
		case 0xDB: 
				RM692D9_WriteToRAM(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);
		break;			
		case 0xAB: 
				RM692D9_qiangqie(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);
		break;	
		default:break;
	}			
}




